package com.saikuo.service.service.impl;

import com.saikuo.service.api.ApiResultModel;
import com.saikuo.service.api.ErrorCode;
import com.saikuo.service.constant.ContextConfig;
import com.saikuo.service.dao.IStoreInfoDao;
import com.saikuo.service.model.StoreInfo;
import com.saikuo.service.service.IStoreInfoService;
import com.saikuo.service.utils.Distance;
import com.saikuo.service.vo.StoreInfoView;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by shanchangyu on 16/10/13.
 */
@Service
public class StoreInfoServiceImpl implements IStoreInfoService {

    @Autowired
    private IStoreInfoDao storeInfoDao;

    @Override
    public ApiResultModel updateStoreInfo(String storeCode, String name, String address, Double lat, Double lng, String cityCode, String areaCode, String mobile, String phone) {
        if (!storeInfoDao.storeCodeExist(storeCode)) {
            return new ApiResultModel(ErrorCode.STORE_NOT_EXIST);
        }
        boolean result = storeInfoDao.updateStoreInfo(storeCode, name, address, lat, lng, cityCode, areaCode, mobile, phone);
        if (!result) {
            return new ApiResultModel(ErrorCode.DATABASE_ERROR);
        }
        return new ApiResultModel(ErrorCode.SUCCESS);
    }

    @Override
    public ApiResultModel getStoreInfoByStoreCode(String storeCode) {
        StoreInfo storeInfo = storeInfoDao.getStoreInfoByStoreCode(storeCode);
        if (storeInfo == null) {
            return new ApiResultModel(ErrorCode.STORE_NOT_EXIST);
        }
        if (storeInfo.getStatus() == 1) {
            return new ApiResultModel(ErrorCode.STORE_FREEZED);
        }
        return new ApiResultModel(storeInfo);

    }

    @Override
    public ApiResultModel getStoresByUserLocation(Double lat, Double lng, String cityCode, String areaCode) {
        List<StoreInfoView> storeInfoViewList = new ArrayList<>();
        List<StoreInfo> storeList = storeInfoDao.getStoresByUserLocationCode(cityCode, areaCode);
        if (CollectionUtils.isNotEmpty(storeList)) {
            for (StoreInfo storeInfo : storeList) {
                Double dist = Distance.GetDistance(lng, lat, storeInfo.getLng(), storeInfo.getLat());
                if (ContextConfig.STORE_DELIVERY_DIS >= dist) {//过滤小于派送范围的药店
                    StoreInfoView storeInfoView = new StoreInfoView();
                    storeInfoView.setId(storeInfo.getId());
                    storeInfoView.setName(storeInfo.getName());
                    storeInfoView.setStoreCode(storeInfo.getStoreCode());
                    storeInfoView.setAddress(storeInfo.getAddress());
                    storeInfoView.setDistance(dist);
                    storeInfoViewList.add(storeInfoView);
                }
            }
        }
        Collections.sort(storeInfoViewList);
        if (!storeInfoViewList.isEmpty()) {
            return new ApiResultModel(storeInfoViewList.get(0));
        }
        return new ApiResultModel();
    }

}
